import http from './require.js';
const pay = (money, id) => {
	if (!money) return false;
	return new Promise((resolve, reject) => {
		http._post('test/wxpay.do', {
			subjectid: id
		}).then(res => {
			console.log(res)
			let {
				data
			} = res.data;
			let orderno = data.orderno;
			console.log(data)
			uni.requestPayment({
				provider: 'wxpay',
				timeStamp: data.timeStamp,
				nonceStr: data.nonceStr,
				package: data.package,
				signType: data.signType,
				paySign: data.paySign,
				success: function(res) {
					let texts = JSON.stringify(res);
					let obj = {
						texts: texts,
						orderno: orderno
					};
					resolve(obj)
				},
				fail: function(err) {
					uni.showToast({
						title: '支付失败',
						duration: 2000,
						icon: 'none'
					})
					// reject(err);
				}
			});
		})
	})
}
const wxcheckpay = (orderno) => {

	console.log('定时器请求')
	return new Promise((resolve, reject) => {
		http._post('test/wxcheckpay.do', {
			orderno: orderno
		}).then(res => {
			if (res.data.issuccess == 1) {
				resolve(res)
			}
		})
	})
}
// 登录授权
const register = (e) => {
	console.log('e', e)
	let that = this;
	return new Promise((resolve, reject) => {
		let that = this;
		let obj = {};
		if (e.detail.userInfo) {
			uni.login({
				success: res => {
					console.log(res)
					if (res.code) {
						uni.getUserInfo({
							provider: 'weixin',
							success: function(infoRes) {
								let {
									userInfo
								} = infoRes;
								// uni.setStorageSync('userInfo', userInfo); 
								http._login(res.code, infoRes.encryptedData, infoRes.iv).then(res => {
									if (res.data.issuccess == 1) {
										let {
											data
										} = res.data;
										console.log('登录结果', data)
										uni.setStorageSync('userInfo', userInfo);
										uni.setStorageSync('uid', data.userid);
										uni.setStorageSync('token', data.token);

										obj.data = data;
										obj.isLogin = true;
										resolve(obj)
									}
									console.log(res)
								})
							}
						})
					}
				}
			})
		} else {

		}
	})

}
export default {
	pay,
	wxcheckpay,
	register
}
